//
//  AppAnalysis.swift
//  App_Common_Analysis
//
//  Created by 蒋晨成 on 2021/12/27.
//

import AppCenterAnalytics
import Center_Key
import Foundation
import Service
import UMCommon

public enum AppAnalysis {}

extension AppAnalysis: TypeName {}

private extension AppAnalysis {
    static let queue = DispatchQueue(label: typeName, qos: .background)
}

public extension AppAnalysis {
    static func bootstrap() {
        queue.async {
            UMConfigure.initWithAppkey(KeyCenter.umeng.appID, channel: nil)
        }
    }
}

public extension AppAnalysis {
    static func beginLogPageView(name: String) {
        queue.async {
            MobClick.beginLogPageView(name)
            Analytics.trackEvent("PageBegin", withProperties: ["name": name])
        }
    }

    static func endLogPageView(name: String) {
        queue.async {
            MobClick.endLogPageView(name)
            Analytics.trackEvent("PageEnd", withProperties: ["name": name])
        }
    }
}

public extension AppAnalysis {
    static func event(id: String, label: String? = nil) {
        queue.async {
            MobClick.event(id, label: label)
        }
    }

    static func event(id: String, attributes: [AnyHashable: Any]? = nil, counter: Int32 = 1) {
        queue.async {
            MobClick.event(id, attributes: attributes, counter: counter)
        }
    }

    static func event(id: String, label: String? = nil, durations: Int32) {
        queue.async {
            MobClick.event(id, label: label, durations: durations)
        }
    }

    static func event(id: String, attributes: [AnyHashable: Any]? = nil, durations: Int32) {
        queue.async {
            MobClick.event(id, attributes: attributes, durations: durations)
        }
    }

    static func beginEvent(id: String, label: String? = nil) {
        queue.async {
            MobClick.beginEvent(id, label: label)
        }
    }

    static func endEvent(id: String, label: String? = nil) {
        queue.async {
            MobClick.endEvent(id, label: label)
        }
    }

    static func beginEvent(id: String, primaryKey: String, attributes: [AnyHashable: Any]? = nil) {
        queue.async {
            MobClick.beginEvent(id, primarykey: primaryKey, attributes: attributes)
        }
    }

    static func endEvent(id: String, primaryKey: String) {
        queue.async {
            MobClick.endEvent(id, primarykey: primaryKey)
        }
    }
}
